package com.fanben100.util;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;

import javax.swing.JOptionPane;

import com.sun.awt.AWTUtilities;

public class PropertyUtils {
	private static String filePath;
	private static Properties props = null;

	// public synchronized static String readValue(String fp, String key,
	// String defaultValue) {
	// String value = null;
	// if (props == null || !fp.equals(filePath)) {
	// props = new Properties();
	// InputStream in = null;
	// try {
	// File f = new File(fp);
	// if (f.exists()) {
	// in = new BufferedInputStream(new FileInputStream(fp));
	// props.load(in);
	// }
	// } catch (FileNotFoundException e) {
	// e.printStackTrace();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// }
	// if (props != null) {
	// value = props.getProperty(key);
	// }
	// if (value == null) {
	// if (defaultValue != null) {
	// value = defaultValue;
	// }
	// }
	// filePath = fp;
	// return value;
	// }

	/**
	 * Return the thread context class loader if available. Otherwise return
	 * null.
	 * 
	 * The thread context class loader is available for JDK 1.2 or later, if
	 * certain security conditions are met.
	 * 
	 * @throws Exception
	 * 
	 * @exception LogConfigurationException
	 *                if a suitable class loader cannot be identified.
	 */
	private static ClassLoader getContextClassLoader() {
		ClassLoader classLoader = null;
		return PropertyUtils.class.getClassLoader();
//		if (classLoader == null) {
//			try {
//				// Are we running on a JDK 1.2 or later system?
//				Method method = Thread.class.getMethod("getContextClassLoader",
//						null);
//
//				// Get the thread context class loader (if there is one)
//				try {
//					classLoader = (ClassLoader) method.invoke(
//							Thread.currentThread(), null);
//				} catch (IllegalAccessException e) {
//					; // ignore
//				} catch (InvocationTargetException e) {
//					/**
//					 * InvocationTargetException is thrown by 'invoke' when the
//					 * method being invoked (getContextClassLoader) throws an
//					 * exception.
//					 * 
//					 * getContextClassLoader() throws SecurityException when the
//					 * context class loader isn't an ancestor of the calling
//					 * class's class loader, or if security permissions are
//					 * restricted.
//					 * 
//					 * In the first case (not related), we want to ignore and
//					 * keep going. We cannot help but also ignore the second
//					 * with the logic below, but other calls elsewhere (to
//					 * obtain a class loader) will trigger this exception where
//					 * we can make a distinction.
//					 */
//					if (e.getTargetException() instanceof SecurityException) {
//						; // ignore
//					} else {
//						// ignore
//						// throw new Exception(
//						// "Unexpected InvocationTargetException"
//						// + e.getTargetException());
//					}
//				}
//			} catch (NoSuchMethodException e) {
//				// Assume we are running on JDK 1.1
//				; // ignore
//			}
//		}
//
//		if (classLoader == null) {
//			System.out.println("classLoader==null");
//			// classLoader = PropertyUtils.class.getClassLoader();
//			classLoader = Thread.currentThread().getContextClassLoader();
//		}
//
//		// Return the selected class loader
//		return classLoader;
	}

	public static InputStream getResourceAsStream(final String name) {
		return (InputStream) AccessController
				.doPrivileged(new PrivilegedAction() {
					public Object run() {
						ClassLoader threadCL = getContextClassLoader();

						if (threadCL != null) {
							return threadCL.getResourceAsStream(name);
						} else {
							return ClassLoader.getSystemResourceAsStream(name);
						}
					}
				});
	}

	public static URL getResourceURL(final String name) {
		return AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
				ClassLoader threadCL = getContextClassLoader();

				if (threadCL != null) {
					return threadCL.getResource(name);
				} else {
					return ClassLoader.getSystemResource(name);
				}
			}
		});
	}

//	public static URL getImageURL(String name) {
//		URL imgURL= getResourceURL(File.separator + "img" + File.separator + name);
//		//JOptionPane.showMessageDialog(null,imgURL);
//		return imgURL;
//	}
//
//	public static URL getIconURL(String name) {
//		return getResourceURL(File.separator + "img" + File.separator + "icon"
//				+ File.separator + name);
//	}
//
//	public static URL getConfigURL(String name) {
//		return getResourceURL(File.separator + "config" + File.separator + name);
//	}
	
//	public static void main(String[] args) {
//		System.out.println(getResourceURL("\\img"));
//		System.out.println(getImageURL("logo.gif"));
//		System.out.println(new PropertyUtils().getClass().getResource("/"));
//	}
}